added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBXPath / MainModule.vb
blob9cb4095aa8d122ce7e46a88d76dd86cf34434285
1 '****************************** Module Header ******************************\
2 ' Module Name: Program.cs
3 ' Project: VBXPath
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' This sample project shows how to use XPathDocument class to load the XML file
7 ' and manipulate. It includes two main parts, XPathNavigator usage and XPath
8 ' Expression usage. The first part shows how to use XPathNavigator to navigate
9 ' through the whole document, read its content. The second part shows how to
10 ' used XPath expression to filter information and select it out.
12 ' This source is subject to the Microsoft Public License.
13 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 ' All other rights reserved.
16 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
17 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
18 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
19 '***************************************************************************/
21 #Region "Imports directives"
23 Imports System.Xml.XPath
25 #End Region
28 Module MainModule
30 Sub Main()
31 'Initialize XPathDocument and XPathNavigator
32 Dim nav As XPathNavigator
33 Dim xPathNavigator As XPathNavigator = New XPathDocument("books.xml").CreateNavigator
35 'Navigate through the document
36 xPathNavigator.MoveToRoot()
37 xPathNavigator.MoveToFirstChild()
38 If ((xPathNavigator.NodeType = XPathNodeType.Element) AndAlso xPathNavigator.HasChildren) Then
39 xPathNavigator.MoveToFirstChild()
41 If xPathNavigator.HasAttributes Then
42 Console.WriteLine(("Book ID: " & xPathNavigator.GetAttribute("id", "")))
43 End If
44 If xPathNavigator.HasChildren Then
45 xPathNavigator.MoveToFirstChild()
47 Console.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), xPathNavigator.Name, xPathNavigator.Value)
48 Loop While xPathNavigator.MoveToNext
49 xPathNavigator.MoveToParent()
50 End If
51 Loop While xPathNavigator.MoveToNext
52 End If
54 'Use XPath Expression to select out the book with ID bk103
55 Console.WriteLine("Use XPath Expression to select out the book with ID bk103:")
56 Dim expression As XPathExpression = xPathNavigator.Compile("/catalog/book[@id='bk103']")
57 Dim iterator As XPathNodeIterator = xPathNavigator.Select(expression)
58 Do While iterator.MoveNext
59 nav = iterator.Current.Clone
60 Console.WriteLine(("Book ID: " & nav.GetAttribute("id", "")))
61 If nav.HasChildren Then
62 nav.MoveToFirstChild()
64 Console.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav.Name, nav.Value)
65 Loop While nav.MoveToNext
66 End If
67 Loop
69 'Use XPath Expression to select out all books whose price are more than 10
70 Console.WriteLine(ChrW(13) & ChrW(10) & "Use XPath Expression to select out all books whose price are more than 10:")
71 expression = xPathNavigator.Compile("/catalog/book[price>10]")
72 iterator = xPathNavigator.Select(expression)
73 Do While iterator.MoveNext
74 nav = iterator.Current.Clone
75 Console.WriteLine(("Book ID: " & nav.GetAttribute("id", "")))
76 If nav.HasChildren Then
77 nav.MoveToFirstChild()
79 If (nav.Name = "title") Then
80 Console.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav.Name, nav.Value)
81 End If
82 If (nav.Name = "price") Then
83 Console.Write(ChrW(9) & "{0}:" & ChrW(9) & "{1}" & ChrW(13) & ChrW(10), nav.Name, nav.Value)
84 End If
85 Loop While nav.MoveToNext
86 End If
87 Loop
89 'Use XPath Expression to calculate the average price of all books.
90 Console.WriteLine(ChrW(13) & ChrW(10) & "Use XPath Expression to calculate the average price of all books:")
91 expression = xPathNavigator.Compile("sum(/catalog/book/price) div count(/catalog/book/price)")
92 Dim averagePrice As String = xPathNavigator.Evaluate(expression).ToString
93 Console.WriteLine("The average price of the books are {0}", averagePrice)
95 'End. Read a char to exit
96 Console.WriteLine("Input any key to quit the sample application")
97 Console.ReadLine()
99 End Sub
101 End Module